package net.coldsky.common.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Propties文件读写工具类
 * @author C
 * */
public abstract class PropertySerializer {

	protected final static Log log = LogFactory.getLog(PropertySerializer.class);

	/**
	 * @return 在文件 fileName 中与 key 向匹配的 value 值
	 * */
	public static String readPropertyFromFile(String fileLocation, String key){
		Properties p =readPropertiesFromFile(fileLocation);
//		Assert.notNull(p, "文件路径 [" + fileLocation + "] is error!");
		return p.getProperty(key);
	}

	/**
	 * 从文件中读取配置信息
	 * @param fileLocation @see FileUtils.convertToFile(String)
	 * */
	public static Properties readPropertiesFromFile(String fileLocation){
		File file= FileUtils.parseFile(fileLocation);
		if(!(file.exists() && file.canRead())){
			String msg = " file.exists() && file.canRead() == false , location ["+file+"]!";
			throw new IllegalArgumentException(msg);
		}
		return readPropertiesFromFile(file);
	}

	/** (读) */
	public static Properties readPropertiesFromFile(File file){
		Properties p = null;
		InputStream inStream = null;
		try {
			p = new Properties();
			inStream = new FileInputStream(file);
			p.load(inStream);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}finally{
			IOUtils.close(inStream);
		}
		return p;
	}

	/** (写) 此方法不是增量增加 相当于写入一个新的内容到文件*/
	public static void savePropertiesToFile(String fileLocation, Properties props){
		File file= FileUtils.parseFile(fileLocation);
		savePropertiesToFile(file, props);
	}

	/** (写) 此方法不是增量增加 相当于写入一个新的内容到文件*/
	public static void savePropertiesToFile(File file, Properties props){
		OutputStream outStream = null;
		try {
			outStream= new FileOutputStream(file);
			props.store(outStream, "");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}finally{
			IOUtils.close(outStream);
		}
	}

	public static void main(String[] args) {
		Properties p = readPropertiesFromFile("classpath:/copyOflog4j.properties");
		log.debug(p);
//		log.debug(getPropertiesFromFile("net/123.txt"));
//		String v = getPropertyFromFile("net/1p.txt","");

		//setPropertiesToFile("net/123.txt", p, "fdsafsd");

//		savePropertyToFile("net/123.txt","save*", "你好1 PROPAGATION_REQUIRED save");
//		savePropertyToFile("net/123.txt","remove*", "PROPAGATION_REQUIRED r");
//		savePropertyToFile("net/123.txt","update*", "PROPAGATION_REQUIRED u");
//		savePropertyToFile("net/123.txt","load*", "PROPAGATION_REQUIRED,readOnly l");
//		savePropertyToFile("net/123.txt","find*", "PROPAGATION_REQUIRED,readOnly f");
//		savePropertyToFile("net/123.txt","get*", "PROPAGATION_REQUIRED,readOnly g");

//		log.debug(readPropertyFromFile("net/123.txt","save*"));
//		log.debug(readPropertyFromFile("net/123.txt","remove*"));
//		log.debug(readPropertyFromFile("net/123.txt","update*"));
//		log.debug(readPropertyFromFile("net/123.txt","load*"));
//		log.debug(readPropertyFromFile("net/123.txt","find*"));
//		log.debug(readPropertyFromFile("net/123.txt","get*"));

	}
}
